package jsyf.oa.dao.wechat.service_otherCompany;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.system.UserEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by jjx on 2018/3/28.
 */
@Repository
public class CompanyMessageDao extends BaseDao {

    public CompanyMessageDao() {
        super.iBaseEntity = new UserEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, UserEntity.class);
    }

    public List<JSONObject> getAllUsers(String condition, Object[] params) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n");
        sb.append("id,\n");
        sb.append("name,\n");
        sb.append("tel,\n");
        sb.append("sex,\n");
        sb.append("roleName,\n");
        sb.append("parentName,\n");
        sb.append("wechatDepartId\n");
        sb.append("FROM V_SYS_USER\n");
        sb.append("WHERE 1=1\n");
        sb.append(condition + "\n");
        sb.append("AND DELFLAG=0 AND JOBSTATE!=1\n");
        super.appendPageSql(sb, 1, Integer.MAX_VALUE);
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), params);
        return jsonList;
    }

    public List<JSONObject> getAllDeparts(String condition, Object[] params) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT\n");
        sb.append("NAME,\n");
        sb.append("PARENT_ID,\n");
        sb.append("WECHAT_ID\n");
        sb.append("FROM T_SYS_DEPART\n");
        sb.append("WHERE 1=1\n");
        sb.append(condition + "\n");
        sb.append("AND DEL_FLAG=0\n");
        sb.append("ORDER BY\n");
        sb.append("T_SYS_DEPART.SORT_NO\n");
        super.appendPageSql(sb, 1, Integer.MAX_VALUE);
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), params);
        return jsonList;
    }

    //获取现有的部门，更新wechatId
    public Integer updateAllDeparts(String condition) {
        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE T_SYS_DEPART\n");
        sb.append("SET WECHAT_ID = (\n");
        sb.append("SELECT\n");
        sb.append("rownum\n");
        sb.append("FROM\n");
        sb.append("(SELECT\n");
        sb.append("id,\n");
        sb.append("@rownum :=@rownum +1 AS rownum\n");
        sb.append("FROM\n");
        sb.append("T_SYS_DEPART,\n");
        sb.append("(SELECT @rownum :=1)r\n");
        sb.append("WHERE\n");
        sb.append("1 = 1\n");
        sb.append("AND DEL_FLAG = 0\n");
        sb.append(condition + "\n");
        sb.append("ORDER BY\n");
        sb.append("T_SYS_DEPART.`PARENT_ID`,\n");
        sb.append("T_SYS_DEPART.SORT_NO\n");
        sb.append(")row\n");
        sb.append("WHERE\n");
        sb.append("row.id = T_SYS_DEPART.id)\n");
        return this.getJdbcTemplate().update(sb.toString());
    }

    public String getWechatIdByParentId(String parentId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT \n");
        sb.append("WECHAT_ID \n");
        sb.append("FROM T_SYS_DEPART \n");
        sb.append("WHERE  ID=?\n");
        super.appendPageSql(sb, 1, Integer.MAX_VALUE);
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), new Object[]{parentId});
        return jsonList.get(0).getString("WECHAT_ID");
    }

    public JSONObject getCompanyMessageByCompanyId(String parentId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT \n");
        sb.append("t_wechat_company.ID, \n");
        sb.append("t_wechat_company.COMPANY_CORPID, \n");
        sb.append("t_wechat_company.CONTACTS_SECRET, \n");
        sb.append("t_wechat_company.APPLICATION_ONE_AGENTID, \n");
        sb.append("t_wechat_company.APPLICATION_ONE_SECRET \n");
        sb.append("FROM t_wechat_company \n");
        sb.append("WHERE t_wechat_company.COMPANY_ID =? \n");
        super.appendPageSql(sb, 1, Integer.MAX_VALUE);
        List<JSONObject> jsonList = super.queryForJsonList(sb.toString(), new Object[]{parentId});
        return jsonList.get(0);
    }

}
